import {
  getCouponConfigInfo,
  submitCoupon, 
} from '../../../utils/networkRequest.js'
import {errorMsg} from '../../../utils/tool.js'
// pages/couponmanage/couponmanage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponTypeIndex: 0, //优惠券类型索引
    checkTimeType: 1, //1固定时间 2领取后算时间
    couponTypes: [], //优惠券类型列表
    beginTime: "", //优惠券有效期开始时间
    endTime: "", //优惠券有效期结束时间    
    multiArray: [],
    multiIndex: ["", ""],
    title: "", //优惠券标题
    money: "", //优惠券金额
    disCount: "", //优惠券折扣条件 满多少可用
    totalNum: "", //优惠券总数
    oneNum: "", //一个人可以领取的数量
    dayNum: "", //有效期天数
    remark: "", //描述

    isEdit:false,//是否是编辑优惠券
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
   
    getCouponConfigInfo("", (r) => {
      if (r.code == 0) { 
        if (options.isEdit){
          let couponItem = JSON.parse(options.item) 
        //  console.log(couponItem)
        //  debugger;
          self.setData({
            couponTypes: r.data.types,
            multiArray: r.data.cateogyItems,
            // isEdit:true,
            // couponTypeIndex: (couponItem.type_id-1),
            // money: couponItem.money,
            // disCount:couponItem.threshold,
            // totalNum: couponItem.total,
            // oneNum:couponItem.max,
            // remark:couponItem.memo,
            // beginTime: couponItem.start_at,
            // endTime: couponItem.endTime,

          })
        }else{
          self.setData({
            couponTypes: r.data.types,
            multiArray: r.data.cateogyItems
          })
        }
      
      }
    })
  },
  //标题修改
  titleChange: function(e) {
    this.setData({
      title: e.detail.detail.value
    })
  },
  //面值修改
  moneyChange: function(e) {
    this.setData({
      money: e.detail.detail.value
    })
  },
  //折扣条件：满多少可用
  disCountChange: function(e) {
    this.setData({
      disCount: e.detail.detail.value
    })
  },
  //优惠券总数
  totalNumChange: function(e) {
    this.setData({
      totalNum: e.detail.detail.value
    })
  },
  //优惠券备注
  remarkChange: function(e) {
    this.setData({
      remark: e.detail.detail.value
    })
  },
  //每人可领数
  oneNumChange: function(e) {
    this.setData({
      oneNum: e.detail.detail.value
    })
  },
  //优惠券类型修改
  bindCouponTypeChange: function(e) {
    this.setData({
      couponTypeIndex: e.detail.value,
      money: "",
      multiIndex: ["", ""],
      disCount: "",
    })
  },
  //获取领取后的有效期天数
  getDayNum: function(e) {
    this.setData({
      dayNum: e.detail.value
    })
  },
  //选择切换
  radioChange: function(e) {
    this.setData({
      checkTimeType: e.detail.value
    })
  },
  //获取优惠券有效期开始时间
  bindBeginTimeChange: function(e) {
    this.setData({
      beginTime: e.detail.value
    })
  },
  //获取优惠券有效期结束时间
  bindEndTimeChange: function(e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  bindMultiPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function(e) {
    let self = this;
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;

    if (e.detail.column == 0) {
      let id = self.data.multiArray[0][e.detail.value].id;
      getCouponConfigInfo(id, (r) => {
        if (r.code == 0) {
          data.multiArray[1] = r.data.cateogyItems[1];
          data.multiIndex[1] = 0;
          self.setData(data);
        } else {
          errorMsg(r.message)
        }
      })

    }
  },
  //创建优惠券
  submitCreate: function() {

    let {
      couponTypeIndex,
      checkTimeType,
      couponTypes,
      beginTime,
      endTime,
      multiArray,
      multiIndex,
      title,
      money,
      disCount,
      totalNum,
      oneNum,
      dayNum,
      remark
    } = this.data;

    let itemId;
    if (multiIndex[1] === "") {
      itemId = ""
    } else {
      itemId = multiArray[1][multiIndex[1]].id;
    }


    if (checkTimeType == 1) {
      //固定时间  
      submitCoupon("", title, itemId, oneNum, money, disCount, beginTime, endTime, totalNum, "", remark, couponTypes[couponTypeIndex].id, (r) => {
        if (r.code == 0) {
          wx.navigateBack();
        } else {
          errorMsg(r.message)
        }

      })
    } else if (checkTimeType == 2) {
      //领取后算时间
      submitCoupon("", title, itemId, oneNum, money, disCount, beginTime, "", "", dayNum, remark, couponTypes[couponTypeIndex].id, (r) => {
        if (r.code == 0) {
          wx.navigateBack();
        } else {
          errorMsg(r.message)
        }
      })
    }


  }
})